import time, logging, json
import tinynl

logging.basicConfig()

s = tinynl.Server(8000, protocol=tinynl.NEWLINE)
clients = []
while True:
  for c in s.new_clients():
    clients.append(c)

  for c in clients[:]: # make copy of clients, so we can remove dead clients.
    try:
      c.send(json.dumps(dict(left=.1, right=.3, text='blaaa\naat')))
      for msg in c.get():
        print 'received: %s.' % msg
        c.send(msg.upper())
    except tinynl.ConnectionError:
      clients.remove(c)
  time.sleep(.1)
